package com.atgion.common.log.aspect;

import com.atgion.common.log.annoyation.Log;
import com.atgion.common.log.service.AsyncOperLogService;
import com.atgion.common.log.utils.LogUtil;
import com.atgion.spzx.model.entity.system.SysOperationLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect  //表示切面类
@Component
@Slf4j
public class LogAspect {

    @Autowired
    private AsyncOperLogService asyncOperLogService;

    // 环绕通知
    @Around(value = "@annotation(sysLog)")
    public Object doAroundAdvice(ProceedingJoinPoint joinPoint , Log sysLog) {

        SysOperationLog sysOperLog = new SysOperationLog() ;
        LogUtil.beforeHandleLog(sysLog , joinPoint , sysOperLog) ;
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
            LogUtil.afterHandlLog(sysLog , proceed , sysOperLog , 0 , null) ;
        } catch (Throwable e) {
            e.printStackTrace();
            LogUtil.afterHandlLog(sysLog , proceed , sysOperLog , 1 , e.getMessage()) ;
            throw new RuntimeException();
        }

        // 保存日志数据
        asyncOperLogService.saveSysOperLog(sysOperLog);
        return proceed ;
    }
}